package team3132.utils;

import edu.wpi.first.wpilibj.Timer;

/**
 * A class to check the time between successive calls.
 * This allows us to easily check where there are long delays within the code.
 *
 * If the called functions are NULL the java compiler SHOULD remove the call making this class a
 * no-op. Keep track of time between call for this instance
 */
public class TimeDelayCheck {

  private double lastTime;
  private double maxTime;     // maximum time between samples
  private Trace trace;

  public TimeDelayCheck(String name, double maxTime) {
    lastTime = Timer.getFPGATimestamp();
    trace = new Trace(name);
    this.maxTime = maxTime;
  }

  public void initTimeCheck() {
    lastTime = Timer.getFPGATimestamp();
  }

  public void checkTimeCheck() {
    double now = Timer.getFPGATimestamp();
    if (now - lastTime > maxTime) {
      // too long
      trace.trace("Time Delay: " + (now - lastTime) + " seconds");
    }
    lastTime = now;
  }
}